package com.taptech.doufu.presenter.novel

import android.app.Activity
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.taptech.doufu.model.novel.NovelCategoryModel
import com.taptech.doufu.model.novel.bean.NovelCategoryGson
import com.taptech.doufu.net.retrofit.api.BaseSubscriber
import com.taptech.doufu.presenter.BasePresenter

class NovelCategoryPresenter(val activity: Activity, mView:INovelCategoryView, val url:String):BasePresenter<INovelCategoryView>(activity,mView) {

    private val model = NovelCategoryModel(activity)
    private var last = 0
    var total = 0

    var countPosition = 0
    var progressPosition = 0
    var statePosition = 0
    var orderPosition = 0
    var subTag = ""

    fun getNovelCategoryList(isRefresh:Boolean){
        getNovelCategoryList(isRefresh,null)
    }



        /**
     * 获取分类数据，需要分页
     */
    fun getNovelCategoryList(isRefresh:Boolean,tag:String?){
        if (isRefresh){
            last = 0
        }
        if (tag != null) {
            subTag = tag
        }

        model.getNovelCategoryList(url,last,getFormatData(),subTag,object :BaseSubscriber<NovelCategoryGson>(){
            override fun onNext(t: NovelCategoryGson?) {
                super.onNext(t)
                if (t!=null){
                    last = t.last
                    total = t.total
                }
                mView.getNovelCategoryListResult(isRefresh, t?.list ?: mutableListOf())
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
                mView.getNovelCategoryListResult(isRefresh, mutableListOf())
            }
        })
    }


    fun getFormatData(): String {
        val json = JSONObject()
        val whereArray = JSONArray()
        whereArray.add(getCountJson())
        whereArray.add(getProgressJson())
        whereArray.add(getStateJson())
        json["where"] = whereArray
        val orderArray = JSONArray()
        orderArray.add(getOrderJson())
        json["order"] = orderArray
        return json.toJSONString()
    }


    fun getOrderJson(): JSONObject {
        val jsonObject = JSONObject()

        when(orderPosition){
            0->{
                jsonObject["key"] = "-1"
                jsonObject["title"] = "综合排序"
            }
            1->{
                jsonObject["key"] = "tl|collect_times"
                jsonObject["title"] = "收藏量"
            }
            2->{
                jsonObject["key"] = "cs|compex_score"
                jsonObject["title"] = "综合分"
            }
            3->{
                jsonObject["key"] = "tl|update_time"
                jsonObject["title"] = "最新更新"
            }
        }
        return jsonObject
    }


    fun getCountJson(): JSONObject {
        val jsonObject = JSONObject()
        jsonObject["key"] = "cs|char_num"

        val array = JSONArray()
        val jsonItem = JSONObject()
        when(countPosition){
            0->{
                jsonItem["key"] = "-1"
                jsonItem["title"] = "不限"
            }
            1->{
                jsonItem["key"] = "0-200000"
                jsonItem["title"] = "20万以下"
            }
            2->{
                jsonItem["key"] = "200000-500000"
                jsonItem["title"] = "20~50万"
            }
            3->{
                jsonItem["key"] = "500000-1000000"
                jsonItem["title"] = "50~100万"
            }
        }
        array.add(jsonItem)
        jsonObject["list"] = array
        jsonObject["title"]  = "数字"
        return jsonObject
    }



    fun getProgressJson(): JSONObject {
        val jsonObject = JSONObject()
        jsonObject["key"] = "tl|is_finished"

        val array = JSONArray()
        val jsonItem = JSONObject()
        when(progressPosition){
            0->{
                jsonItem["key"] = "-1"
                jsonItem["title"] = "不限"
            }
            1->{
                jsonItem["key"] = "0"
                jsonItem["title"] = "连载"
            }
            2->{
                jsonItem["key"] = "1"
                jsonItem["title"] = "完结"
            }
        }
        array.add(jsonItem)
        jsonObject["list"] = array
        jsonObject["title"]  = "进度"
        return jsonObject
    }


    fun getStateJson(): JSONObject {
        val jsonObject = JSONObject()
        jsonObject["key"] = "tl|is_vip"

        val array = JSONArray()
        val jsonItem = JSONObject()
        when(statePosition){
            0->{
                jsonItem["key"] = "-1"
                jsonItem["title"] = "不限"
            }
            2->{
                jsonItem["key"] = "0"
                jsonItem["title"] = "免费"
            }
            1->{
                jsonItem["key"] = "1"
                jsonItem["title"] = "收费"
            }
        }
        array.add(jsonItem)
        jsonObject["list"] = array
        jsonObject["title"]  = "状态"
        return jsonObject
    }
}